const Base = require('./base.js');
const ErrCode = require('../utils/errCode');
const Status = require('../utils/status');

module.exports = class extends Base {

    async getAllClassAction() {
        let rowDatas = await this.model('class').where({ userid: this.userInfo.id }).select();
        for (let i = 0; i < rowDatas.length; i++) {
            rowDatas[i].subjectList = await this.model('subject').where({ classid: rowDatas[i].id }).select() || [];
        }
        return this.success(rowDatas)

    }
    async updateClassAction() {
        let { classid, classname } = this.post()
        let res = await this.model('class').where({ id: classid, userid: this.userInfo.id }).update({ classname });
        return res > 0 ? this.success(res, Status.ADD_CLASS_SUCCESS) : this.fail(ErrCode.CLASS_UPDATE_FAIL.code, ErrCode.CLASS_UPDATE_FAIL.msg);
    }
    async addClassAction() {
        const { classname } = this.get();
        await this.model('class').add({ classname, userid: this.userInfo.id });
        return this.success({}, Status.ADD_CLASS_SUCCESS);
    }
    async deleteClassByIdAction() {
        const { classid } = this.get();
        await this.model('class').where({ id: classid, userid: this.userInfo.id }).delete();
        await this.model('student').where({ classid }).delete();
        await this.model('subject').where({ classid }).delete();
        await this.model('student_subject').where({ classid }).delete();
        return this.success({}, Status.DELETE_CLASS_SUCCESS);
    }

};